﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.mMall.Public
{
    public partial class mfVehicleSearch : msBsc.Cls.mbPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private void initplace()
        {
            string parentsId = this.GetParamHtValue("parentsId");
            string countryId = this.GetParamHtValue("countryId");
            string cityId = this.GetParamHtValue("cityId");
            string destionId = this.GetParamHtValue("destionId");
            string isAll = this.GetParamHtValue("isAll");
            string pIndex = this.GetParamHtValue("pIndex");
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                if (string.IsNullOrEmpty(countryId))
                {
                    countryId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                string strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""country_select"",'" + countryId + "');");

                ds = cbcc.SelectCountryByParentId(countryId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                if (string.IsNullOrEmpty(cityId))
                {
                    cityId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""city_select"",'" + cityId + "');");

                ds = cbcc.SelectCountryByParentId(cityId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                if (isAll.ToLower() == "true")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["COUNTRY_CITY_ID"] = "";
                    dr["COUNTRY_CITY_NME"] = "全部";
                    ds.Tables[0].Rows.InsertAt(dr, 0);
                }
                if (string.IsNullOrEmpty(destionId))
                {
                    destionId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""destion_select"",'" + destionId + "');");

                GetVehicleList(pIndex, countryId, cityId, destionId);
            }
        }
        private void GetVehicleList(string pindex, string countryId, string cityId, string destionId, bool isAdditional = false)
        {
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicle = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                int pageRows = 15;
                int pageIndex = 0;
                int.TryParse(pindex, out pageIndex);
                int vehicleType = 0;
                int.TryParse(this.GetParamHtValue("vehicleType"), out vehicleType);
                int brand = 0;
                int.TryParse(this.GetParamHtValue("checkBrand"), out brand);
                decimal minVehiclePrice = 0;
                decimal.TryParse(this.GetParamHtValue("minVehiclePrice"), out minVehiclePrice);
                decimal maxVehiclePrice = 0;
                decimal.TryParse(this.GetParamHtValue("maxVehiclePrice"), out maxVehiclePrice);
                int minVehicleSeat = 0;
                int.TryParse(this.GetParamHtValue("minVehicleSeat"), out minVehicleSeat);
                int maxVehicleSeat = 0;
                int.TryParse(this.GetParamHtValue("maxVehicleSeat"), out maxVehicleSeat);
                int serviceType = 0;
                int.TryParse(this.GetParamHtValue("serviceType"), out serviceType);
                DataSet ds = vehicle.GetVehicleForPageview(pageRows, pageIndex, destionId, cityId, countryId, vehicleType, brand, minVehiclePrice, maxVehiclePrice, minVehicleSeat, maxVehicleSeat, serviceType);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    string nodata = @"<dl >暂时没有您查找的数据...</dl>";
                    this.ExecJScript(@"$('#CarList').html('" + nodata + "');$('#xLoading').hide();$('#Div1').hide();");
                    return;
                }
                DataColumn dr = new DataColumn("VEHICLE_BRAND_NAME");
                ds.Tables[0].Columns.Add(dr);
                DataColumn dr1 = new DataColumn("VEHICLE_SERVICE_TYPE_NAME");
                ds.Tables[0].Columns.Add(dr1);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {

                    ds.Tables[0].Rows[i]["VEHICLE_BRAND_NAME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.VehicleBrandEnum), Convert.ToInt32(ds.Tables[0].Rows[i]["VEHICLE_BRAND"].ToString()));

                    ds.Tables[0].Rows[i]["VEHICLE_SERVICE_TYPE_NAME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.ServiceTypeEnum), Convert.ToInt32(ds.Tables[0].Rows[i]["VEHICLE_SERVICE_TYPE"].ToString()));
                }
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 500, "");
                if (isAdditional)
                {
                    this.ExecJScript(@"
                            var _html = $('#car_list_temp').html();
                            laytpl(_html).render(" + retJson + @", function (render) {
                                $('#CarList').append(render);
                                $('#PageCount').html('" + ds.Tables[0].Rows[0]["ROWSCOUNT"].ToString() + @"');
                                $('#xLoading').hide();$('#Div1').show();});");
                }
                else
                {
                    this.ExecJScript(@"
                            var _html = $('#car_list_temp').html();
                            laytpl(_html).render(" + retJson + @", function (render) {
                                $('#CarList').html(render);
                                $('#PageCount').html('" + ds.Tables[0].Rows[0]["ROWSCOUNT"].ToString() + @"');});"
                    );
                }
            }
        }

        private void NextPage()
        {
            string parentsId = this.GetParamHtValue("parentsId");
            string countryId = this.GetParamHtValue("countryId");
            string cityId = this.GetParamHtValue("cityId");
            string destionId = this.GetParamHtValue("destionId");
            string pIndex = this.GetParamHtValue("pIndex");
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                if (string.IsNullOrEmpty(countryId))
                {
                    countryId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                ds = cbcc.SelectCountryByParentId(countryId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                if (string.IsNullOrEmpty(cityId))
                {
                    cityId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                ds = cbcc.SelectCountryByParentId(cityId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                DataRow dr = ds.Tables[0].NewRow();
                dr["COUNTRY_CITY_ID"] = "";
                dr["COUNTRY_CITY_NME"] = "全部";
                ds.Tables[0].Rows.InsertAt(dr, 0);
                if (string.IsNullOrEmpty(destionId))
                {
                    destionId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
            }
            GetVehicleList(pIndex, countryId, cityId, destionId, true);
        }
        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                initplace();
            }
            else if (this.strAction == "NextPage")
            {
                NextPage();
            }
        }
    }
}